/*******************
Table 2. Effect of EITC Generosity on Self-Employment of Single, Less-educated Households, by Uber Availability
*******************/
use "data\hslesshousehold_ubereitc2005_2019", clear

foreach v in incbus002020 inctot2020 {
		replace `v' = `v'/100
}	
local outcome = "selfemp"
	reghdfe `outcome' max_total_credit2020 o.max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_0`outcome' = e(b)
	matrix V_0`outcome' = e(V)
	scalar edf_r0`outcome' = e(df_r)
	scalar n_0`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_0`outcome' = r(mean)
	
foreach outcome in selfemp have_selfempinc incbus002020 inctot2020 anyemployed wagedemp   {
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_`outcome' = r(mean)
}

//Table
cap file close regout
file open regout using "output\tab_eitcuber_alloutcomes.tex", write replace
//Header
file write regout "\begin{tabular}{lccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Self-Employment Outcomes} & Any & Any Wage & Business & Total \\" _n
file write regout "\cmidrule{2-4}"
file write regout "& \multicolumn{2}{c}{Self-Employed} & Business Income$>$0 & Employment & Employment & Income (100s) & Income (100s) \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5 ) & (6) & (7)  \\" _n
file write regout "\midrule" _n
//Table
file write regout "Maximum EITC (1000s)" 
foreach outcome in 0selfemp selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020 {
	file write regout "&" %7.4f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 0selfemp selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020  {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
//Interaction
file write regout "Maximum EITC (1000s)*Uber in MSA & " 
foreach outcome in selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020 {
	file write regout "&" %7.4f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "&" 
foreach outcome in selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020  {
	file write regout "& " "(" %5.4f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n
//Uber
file write regout "Uber in MSA " 
foreach outcome in 0selfemp selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020 {
	file write regout " &" %7.4f (b_`outcome'[1,3])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 0selfemp selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020 {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[3,3]) ) ")"
}
file write regout "\\" _n

file write regout "\\" _n
//Statistics
file write regout "Dependent Mean"
foreach outcome in 0selfemp selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020  {
	file write regout "&" %5.2f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in 0selfemp selfemp have_selfempinc anyemployed wagedemp incbus002020 inctot2020  {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout